var initLazyload = function() {
  // 获取所有需要懒加载的图片
  const lazyImages = document.querySelectorAll('img[data-src]');

  const options = {
    root: null,
    rootMargin: '0px',
    threshold: 0.1 // 当图片至少有10%可见时就开始加载
  };

  const loadImage = (image) => {
    image.setAttribute('src', image.getAttribute('data-src'));
    image.onload = () => {
      image.removeAttribute('data-src');
    };
  };

  const observer = new IntersectionObserver((entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const img = entry.target;
        loadImage(img);
        observer.unobserve(img); // 加载完成之后停止观察
      }
    });
  }, options);

  lazyImages.forEach(lazyImg => {
    observer.observe(lazyImg);
  });
}